package grpc

import (
	"github.com/sirupsen/logrus"
	"github.com/ztx/payment_project/common/genproto/orderpd"
	"github.com/ztx/payment_project/common/genproto/stockpd"
	"golang.org/x/net/context"
)

type StockGRPC struct {
	client stockpd.StockServiceClient
}

func NewStockGRPC(client stockpd.StockServiceClient) *StockGRPC {
	return &StockGRPC{client: client}
}

func (s StockGRPC) CheckIfItemsInStock(ctx context.Context, items []*orderpd.ItemWithQuantity) (*stockpd.CheckIfItemsInStockResponse, error) {
	resp, err := s.client.CheckIfItemsInStock(ctx, &stockpd.CheckIfItemsInStockRequest{Items: items})
	logrus.Info("stock_grpc response", resp)
	return resp, err
}

func (s StockGRPC) GetItems(ctx context.Context, itemIDs []string) ([]*orderpd.Item, error) {
	resp, err := s.client.GetItems(ctx, &stockpd.GetItemsRequest{ItemIDs: itemIDs})
	if err != nil {
		return nil, err
	}
	return resp.Items, nil
}
